using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace com.yoozoo.gta.Art.Editor
{
    public class ObjectsPropertiesEditBox : EditorWindow
    {
        public GameObject rootObject;
        public Light[] allLights;//灯光搜索结果全存在这个列表里
        public MeshRenderer[] allObjects;//物体搜索结果全存在这个列表里

        public Vector3 positionValue;
        public Vector3 rotationValue;
        public Vector3 scaleValue;
        public float intensityValue;
        public MeshRenderer tempMeshrender;
        public bool changeMeshSetting;

        private SerializedObject _spObj_Light;
        private SerializedProperty _spPro_Light;
        private SerializedObject _spObj_Object;
        private SerializedProperty _spPro_Object;
        
        private int _operationType = 0;//运算类型
        private readonly List<string> _operationOptions = new List<string>()
        {
            "相加",
            "相乘"
        };
        private readonly List<int> _operationOptionsIndex = new List<int>()
        {
            0, 1
        };
        private int _objectType = 0;//运算类型
        private readonly List<string> _objectOptions = new List<string>()
        {
            "网格",
            "灯光"
        };
        private readonly List<int> _objectOptionsIndex = new List<int>()
        {
            0, 1
        };
        private Vector2 _scrollViewVector = new Vector2();//用于在面板展示大量搜索结果时，可以滑动列表查看结果
        
        [MenuItem("HRP/物体属性批量修改工具")]
        public static void ShowWindow()
        {
            ObjectsPropertiesEditBox window =
                (ObjectsPropertiesEditBox) GetWindow(typeof(ObjectsPropertiesEditBox));
            window.titleContent.text = "物体属性编辑器";
            window.Show();
            window.Focus();
        }

        private void OnEnable()
        {
            _spObj_Light = new SerializedObject(this);
            _spPro_Light = _spObj_Light.FindProperty("allLights");
            _spObj_Object = new SerializedObject(this);
            _spPro_Object = _spObj_Object.FindProperty("allObjects");
        }

        private void OnGUI()
        {
            GUILayout.Space(10);
            GUI.skin.label.fontSize = 24;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("物 体 属 性 批 量 修 改 工 具");
            GUI.skin.label.fontSize = 12;
            GUI.skin.label.alignment = TextAnchor.MiddleLeft;

            GUILayout.Space(12);

            rootObject = EditorGUILayout.ObjectField("指定场景父节点:", rootObject, typeof(GameObject), true) as GameObject;
            GUILayout.Space(8);
            if (rootObject == null)
            {
                EditorGUILayout.HelpBox("请指定父节点！",MessageType.Info);
            }
            _objectType = EditorGUILayout.IntPopup("选择筛选类型：", _objectType, _objectOptions.ToArray(),
                _objectOptionsIndex.ToArray());
            _operationType = EditorGUILayout.IntPopup("选择运算方式：", _operationType, _operationOptions.ToArray(),
                _operationOptionsIndex.ToArray());

            GUILayout.Space(8);

            if (GUILayout.Button("物体查找器，启动！！", GUILayout.Height(24)))
            {
                if (rootObject == null)
                {
                    UnityEngine.Debug.LogError("物体属性批量修改工具：请指定父级！");
                    return;
                }
                GetAllLights();
            }

            if (allObjects != null || allLights != null)
            {
                if (GUILayout.Button("重置", GUILayout.Height(24)))
                {
                    Clear();
                }

                if (_operationType == 0)
                {
                    positionValue = EditorGUILayout.Vector3Field("Position:", positionValue);
                }
                
                //rotationValue = EditorGUILayout.Vector3Field("Rotation:", rotationValue);
                scaleValue = EditorGUILayout.Vector3Field("Scale:", scaleValue);

                if(_objectType==0)
                {
                    GUILayout.Space(4);
                    tempMeshrender  = EditorGUILayout.ObjectField("指定属性meshRender,点击开始修改后所有列表中的mesh都会按照该meshRender的设置修改，为空则不修改", tempMeshrender, typeof(MeshRenderer), true) as MeshRenderer;
                }

                if (_objectType == 1)
                {
                    GUILayout.Space(4);
                    intensityValue = EditorGUILayout.FloatField("Intensity", intensityValue);
                }

                GUILayout.Space(8);
                if (GUILayout.Button("开始修改！", GUILayout.Height(24)))
                {
                    ChangeProperties();
                }
            }

            _scrollViewVector = EditorGUILayout.BeginScrollView(_scrollViewVector);
            switch (_objectType)
            {
                case 0:
                    EditorGUILayout.PropertyField(_spPro_Object);
                    break;
                case 1:
                    EditorGUILayout.PropertyField(_spPro_Light);
                    break;
            }
            EditorGUILayout.EndScrollView();
        }

        private void GetAllLights()
        {
            switch (_objectType)
            {
                case 0:
                    Clear();
                    allObjects = rootObject.GetComponentsInChildren<MeshRenderer>(true);
                    _spObj_Object.Update();
                    _spObj_Object.ApplyModifiedProperties();
                    break;
                case 1:
                    Clear();
                    allLights = rootObject.GetComponentsInChildren<Light>(true);
                    _spObj_Light.Update();
                    _spObj_Light.ApplyModifiedProperties();
                    break;
            }
        }

        private void ChangeProperties()
        {
            switch (_objectType)
            {
                case 0:
                    switch (_operationType)
                    {
                        case 0:
                            foreach (var objects in allObjects)
                            {
                                objects.transform.position += positionValue;
                                /*objects.transform.Rotate(new Vector3(objects.transform.rotation.x+rotationValue.x,
                                                                            objects.transform.rotation.y+rotationValue.y,
                                                                            objects.transform.rotation.z+rotationValue.z));*/
                                objects.transform.localScale += scaleValue;

                                
                            }
                            break;
                        case 1:
                            foreach (var objects in allObjects)
                            {
                                /*objects.transform.position = new Vector3(objects.transform.position.x * positionValue.x,
                                                                            objects.transform.position.y * positionValue.y,
                                                                            objects.transform.position.z * positionValue.z);*/
                                /*objects.transform.Rotate(new Vector3(objects.transform.rotation.x*rotationValue.x,
                                                                            objects.transform.rotation.y*rotationValue.y,
                                                                            objects.transform.rotation.z*rotationValue.z));*/
                                objects.transform.localScale = new Vector3(objects.transform.localScale.x * scaleValue.x,
                                                                            objects.transform.localScale.y * scaleValue.y,
                                                                            objects.transform.localScale.z * scaleValue.z);
                            }
                            break;
                    }
                    if (tempMeshrender != null) 
                    {
                        Debug.Log("执行修改meshrenderer属性");
                        foreach (var objects in allObjects)
                        {
                            objects.receiveGI = tempMeshrender.receiveGI;
                            //objects.shadowCastingMode = tempMeshrender.shadowCastingMode;
                            //objects.receiveShadows = tempMeshrender.receiveShadows;
                            objects.reflectionProbeUsage = tempMeshrender.reflectionProbeUsage;
                            objects.lightProbeUsage = objects.lightProbeUsage;
                        }
                    }

                    break;
                case 1:
                    switch (_operationType)
                    {
                        case 0:
                            foreach (var objects in allLights)
                            {
                                objects.transform.position += positionValue;
                                /*objects.transform.Rotate(new Vector3(objects.transform.rotation.x+rotationValue.x,
                                                                            objects.transform.rotation.y+rotationValue.y,
                                                                            objects.transform.rotation.z+rotationValue.z));*/
                                objects.transform.localScale += scaleValue;

                                objects.intensity += intensityValue;
                            }
                            break;
                        case 1:
                            foreach (var objects in allLights)
                            {
                                /*objects.transform.Translate(new Vector3(objects.transform.position.x * positionValue.x,
                                                                        objects.transform.position.y * positionValue.y,
                                                                        objects.transform.position.z * positionValue.z));*/
                                /*objects.transform.Rotate(new Vector3(objects.transform.rotation.x*rotationValue.x,
                                                                        objects.transform.rotation.y*rotationValue.y,
                                                                        objects.transform.rotation.z*rotationValue.z));*/
                                objects.transform.localScale = new Vector3(objects.transform.localScale.x * scaleValue.x,
                                                                        objects.transform.localScale.y * scaleValue.y,
                                                                        objects.transform.localScale.z * scaleValue.z);
                                
                                objects.intensity *= intensityValue;
                            }
                            break;
                    }
                    break;
            }
        }

        private void Clear()
        {
            if (allObjects != null)
            {
                allObjects = null;
                _spObj_Object.Update();
            }
            if (allLights != null)
            {
                allLights = null;
                _spObj_Light.Update();
            }
            positionValue = new Vector3(0, 0, 0);
            rotationValue = new Vector3(0, 0, 0);
            scaleValue = new Vector3(0, 0, 0);
            intensityValue = 0.0f;
        }
        
        private void OnDestroy()
        {
            Clear();
        }
    }
}
